package cineplanet.web;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cineplanet.excepcion.DAOExcepcion;
import cineplanet.excepcion.LoginExcepcion;
import cineplanet.modelo.Usuario;
import cineplanet.service.UsuarioService;
import cineplanet.web.UsuarioController;

public class UsuarioController extends MultiActionController {
	public UsuarioController() {

	}

	private UsuarioService usuarioService;

	public UsuarioService getUsuarioService() {
		return usuarioService;
	}

	public void setUsuarioService(UsuarioService usuarioService) {
		this.usuarioService = usuarioService;
	}

	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("/compra_combos");
	}

	@Override
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = null;

		String u = request.getParameter("email");
		String p = request.getParameter("clave");
		System.out.println(u);
		System.out.println(p);
		try {
			Usuario vo = usuarioService.validar(u, p);
			HttpSession session = request.getSession();
			session.setAttribute("USUARIO_ACTUAL", vo);
			mv = new ModelAndView("redirect:/compra_combos.htm");
		} catch (DAOExcepcion ex) {
			System.err.println(ex.toString());
			mv = new ModelAndView("error");
		} catch (LoginExcepcion ex) {
			System.out.println(ex.toString());
			mv = new ModelAndView("error");
		}
		return mv;
	}

}
